#!/bin/bash


base_dir="./"
log_dir="/var/log/yum"
conf_file="${base_dir}/yum_rsync_list.conf"
data_dir="/data/mirrors"
log_rsync_status_file="${log_dir}/rsync_yum_list_status.log"
yum_rsync_list_count=$(cat ${conf_file}|grep -Ev "(^$|^#)"|wc -l)

mkdir -p $log_dir

>${log_rsync_status_file}

## 预处理
for n in `seq 1 $yum_rsync_list_count`
do
    DATE=$(date +%F)
    TIME=$(date +%T)
    date_time="$DATE $TIME"
    yum_name=$(cat ${conf_file}|grep -Ev "(^$|^#)"|awk 'NR=='$n'{print $1}'|xargs)
    yum_rsync_dir=$(cat ${conf_file}|grep -Ev "(^$|^#)"|awk 'NR=='$n'{print $2}'|xargs)
    yum_data_dir=$(cat ${conf_file}|grep -Ev "(^$|^#)"|awk 'NR=='$n'{print $3}'|xargs)
    echo "$yum_name , true , presyncing , $date_time +0800 , 1641989350 , $date_time +0800 , 1641989347 , $date_time +0800 , 1641989350 , $date_time +0800 , 1641996550 , ${yum_rsync_dir} , unknown , $yum_data_dir" >>${log_rsync_status_file}
done

/bin/sh ${base_dir}/yum_status_rsync.sh

## 开始同步
for n in `seq 1 $yum_rsync_list_count`
do
    DATE=$(date +%F)
    TIME=$(date +%T)
    date_time="$DATE $TIME"
    yum_name=$(cat ${conf_file}|grep -Ev "(^$|^#)"|awk 'NR=='$n'{print $1}'|xargs)
    yum_rsync_dir=$(cat ${conf_file}|grep -Ev "(^$|^#)"|awk 'NR=='$n'{print $2}'|xargs)
    yum_data_dir=$(cat ${conf_file}|grep -Ev "(^$|^#)"|awk 'NR=='$n'{print $3}'|xargs)
    echo "$yum_name , true , syncing , $date_time +0800 , 1641989350 , $date_time +0800 , 1641989347 , $date_time +0800 , 1641989350 , $date_time +0800 , 1641996550 , ${yum_rsync_dir} , unknown , $yum_data_dir" >>${log_rsync_status_file}
    /bin/sh ${base_dir}/yum_status_rsync.sh
    mkdir -p ${data_dir}/$yum_name
    rsync -avzuP $yum_rsync_dir $yum_data_dir | tee  ${data_dir}/.help/rsync_yum_${yum_name}_status.html
    if [ $? -eq 0 ];then
        sed -i '/syncing/d' ${log_rsync_status_file}
        echo "$yum_name , true , success , $date_time +0800 , 1641989350 , $date_time +0800 , 1641989347 , $date_time +0800 , 1641989350 , $date_time +0800 , 1641996550 , ${yum_rsync_dir} , unknown  , $yum_data_dir" >>${log_rsync_status_file}
        /bin/sh ${base_dir}/yum_status_rsync.sh
    else
        sed -i '/syncing/d' ${log_rsync_status_file}
        echo "$yum_name , true , failed , $date_time +0800 , 1641989350 , $date_time +0800 , 1641989347 , $date_time +0800 , 1641989350 , $date_time +0800 , 1641996550 , ${yum_rsync_dir} , unknown , $yum_data_dir" >>${log_rsync_status_file}
        /bin/sh ${base_dir}/yum_status_rsync.sh
    fi
done
